package org.test.client.ui.component.btn;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;

import org.test.client.ui.component.BreadcrumbNav;
import org.test.client.ui.util.UIConstants;
import org.test.debug.Debug;

public class LinkButton extends JButton{
	private LinkButton thisLink = this;
	private String linkName = null;
	private boolean useUnderLine = false;

	private Color fontColor = BreadcrumbNav.fontColor;
	private Font txtfont = UIConstants.commonFont;
	
	public LinkButton(String linkName){
		this.linkName = linkName;
		
//	     setBorderPainted(false);
	     setContentAreaFilled(false);
	     this.setText(linkName);
	     this.setFont(txtfont);
//	     this.setToolTipText(linkName.toString());
	     
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		setMargin(new Insets(0, 0, 0, 0));
		
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent mouseevent) {
				useUnderLine = true;
				thisLink.repaint();
			}
			
			@Override
			public void mouseExited(MouseEvent mouseevent) {
				useUnderLine = false;
				thisLink.repaint();
			}
		});
		
		this.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Debug.debug(thisLink.getParent());
			}
		});
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		String urlStr = linkName;
		Graphics2D g2d = (Graphics2D)g;
		
//		Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
//		g2d.setFont(font);
		
		g2d.setColor(fontColor);
		g2d.setFont(txtfont);
		FontMetrics fm = g.getFontMetrics();
		int stringWidth = fm.stringWidth(urlStr);
		int ascent = fm.getAscent();
		int descent = fm.getDescent();
		int tmph = this.getHeight();
		
		int x = 1;
		int y = tmph/2+(ascent - descent)/2;
		g2d.drawString(urlStr, x, y);
		if(useUnderLine){
			y = y+descent;
			g2d.drawLine(x, y, x+stringWidth,y);
			
		}
		
		
	}
	
}

